<?php

class product extends spController
{
    function __construct()
    {
        parent::__construct();
        header("Content-Type: text/html;charset=utf-8");

        $this->gdlink = spClass('link_list')->findAll();
        $sitesystem = spClass("site_system")->find("id=1");

        $comma_separated = explode(',', $sitesystem["skeyword"]);

        $this->config222 = $comma_separated;

        $this->sitesystem = $sitesystem;

        $this->gdlink = spClass('link_list')->findAll();

        $this->banner = spClass('page_img')->findAll('nid=1');

        $this->banner5 = spClass('page_img')->findAll('nid=5');

        $this->hotnewslist = spClass('article_list')->findAll('pid=5 or pid=8 or pid=69', 'id desc limit 20');

        $this->promotegoods = spClass('goods_list')->findAll('id>0 and promote=1', 'sort_id asc,id desc limit 2');


        $this->catlist = spClass('goods_cat')->findAll('pid=0', 'sort_id asc,id desc');

        $cat_select12 = spClass("goods_cat")->findAll("pid=0", "sort_id asc");
        foreach ($cat_select12 as $c_ss => $c_valuee) {
            $cat_select12[$c_ss]['second'] = spClass("goods_cat")->findAll("pid=" . $c_valuee['id'], "sort_id asc");
            foreach ($cat_select12[$c_ss]['second'] as $c_sss => $c_valueee) {
                $cat_select12[$c_ss]['second'][$c_sss]['third'] = spClass("goods_cat")->findAll("pid=" . $c_valueee['id'], "sort_id asc");
            }
        }

        $this->cat_select12 = $cat_select12;
    }


    function cindex()
    {

        $id = empty($_GET['id']) ? 33 : intval($_GET['id']);

        $this->sitesystem = spClass("site_system")->find("id=1");

        $this->catlist = spClass('goods_cat')->findAll('id>0', 'sort_id asc,id desc');

        $this->bbanlicate = spClass("article_cat")->findAll("pid=83", "sort_id asc,id asc");


        $this->prowsname = spClass("goods_cat")->find("id=" . $id, "sort_id asc,id desc");

        $this->display("index/cpr.html");
    }


    function index()
    {
        $id = empty($_GET['id']) ? 33 : intval($_GET['id']);
        $this->sitesystem = spClass("site_system")->find("id=1");
        $this->catlist = spClass('goods_cat')->findAll('id>0', 'sort_id asc,id desc');
        if ($_REQUEST["ssid"]) {
            $this->prowsname = spClass("goods_cat")->find("id=" . $_REQUEST["ssid"], "sort_id asc,id desc");
        } elseif ($_REQUEST["sid"]) {
            $this->prowsname = spClass("goods_cat")->find("id=" . $_REQUEST["sid"], "sort_id asc,id desc");
        } else {
            if ($_REQUEST["bid"]) {
                $this->prowsname = spClass("goods_cat")->find("id=" . $_REQUEST["bid"], "sort_id asc,id desc");
            }
        }
        if ($_REQUEST["bid"] <> "" and $_REQUEST["sid"] == "") {
            $cat_select1g = spClass("goods_cat")->findAll("pid=" . $_REQUEST["bid"], "sort_id asc");
            $this->pcate = spClass("goods_cat")->find("id=" . $_REQUEST["bid"], "sort_id asc");
            foreach ($cat_select1g as $key => $va) {
                $ddjj = spClass("goods_cat")->findAll("pid=" . $va["id"], "sort_id asc");
                if (!empty($ddjj)) {
                    $pcat_select1g[$key] = spClass("goods_cat")->findAll("pid=" . $va["id"], "sort_id asc");
                } else {
                    $vav[] = $va["id"];
                }
            }
            if (!empty($ddjj)) {
                foreach ($pcat_select1g as $key => $vvva) {
                    foreach ($vvva as $vgaab) {
                        $vav[] = $vgaab["id"];
                    }
                }
            }
            $pidall = implode(',', $vav);
            if (empty($pidall)) {
                $pidall = $_REQUEST["bid"];
            }
            $this->bcate = spClass("goods_cat")->find("id=" . $_REQUEST["bid"], "sort_id asc");
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 15)->findAll("pid in ($pidall)", 'sort_id asc,id desc');
            $this->pager = spClass('goods_list')->spPager()->getPager();
        } elseif ($_REQUEST["bid"] <> "" and $_REQUEST["sid"] <> "" and $_REQUEST["ssid"] == "") {
            $this->bcate = spClass("goods_cat")->find("id=" . $_REQUEST["bid"], "sort_id asc");
            $this->scate = spClass("goods_cat")->find("id=" . $_REQUEST["sid"], "sort_id asc");
            $cat_selectuu = spClass("goods_cat")->findAll("pid=" . $_REQUEST["sid"], "sort_id asc");
            foreach ($cat_selectuu as $vaa) {
                $vava[] = $vaa["id"];
            }
            $pidalll = implode(',', $vava);
            if (empty($pidalll)) {
                $pidalll = $_REQUEST["sid"];
            }
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 15)->findAll("pid in ($pidalll)", 'sort_id asc,id desc');
            $this->pager = spClass('goods_list')->spPager()->getPager();
        } elseif ($_REQUEST["bid"] <> "" and $_REQUEST["sid"] <> "" and $_REQUEST["ssid"] <> "") {
            $this->bcate = spClass("goods_cat")->find("id=" . $_REQUEST["bid"], "sort_id asc");
            $this->scate = spClass("goods_cat")->find("id=" . $_REQUEST["sid"], "sort_id asc");
            $this->sscate = spClass("goods_cat")->find("id=" . $_REQUEST["ssid"], "sort_id asc");
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 15)->findAll("pid=" . $_REQUEST["ssid"], 'sort_id asc,id desc');
            $this->pager = spClass('goods_list')->spPager()->getPager();
        } else {
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 15)->findAll("1=1", 'sort_id asc,id desc');
            $this->pager = spClass('goods_list')->spPager()->getPager();
        }
        $this->newslist = spClass('article_list')->findAll('pid=5', 'id desc limit 10');
        $this->display("index/pr.html");
    }


    function indexNews()
    {

        $this->sitesystem = spClass("site_system")->find("id=1");

        $this->catlist = spClass('goods_cat')->findAll('id>0', 'sort_id asc,id desc');

        $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 15)->findAll("hot=1", 'sort_id asc,id desc');

        $this->pager = spClass('goods_list')->spPager()->getPager();

        $this->newslist = spClass('article_list')->findAll('pid=5', 'id desc limit 10');

        $this->display("index/pr.html");
    }

    function tjindex()
    {

        $id = empty($_GET['id']) ? 33 : intval($_GET['id']);

        $this->sitesystem = spClass("site_system")->find("id=1");

        $this->catlist = spClass('goods_cat')->findAll('id>0', 'sort_id asc,id desc');

        $this->prowsname = spClass("goods_cat")->find("id=" . $id, "sort_id asc,id desc");


        if ($_REQUEST["bid"] <> "" and $_REQUEST["sid"] == "") {
            $cat_select1g = spClass("goods_cat")->findAll("pid=" . $_REQUEST["bid"], "sort_id asc");

            $this->pcate = spClass("goods_cat")->find("id=" . $_REQUEST["bid"], "sort_id asc");

            foreach ($cat_select1g as $va) {

                $vav[] = $va["id"];
            }
            $pidall = implode(',', $vav);
            if (empty($pidall)) {
                $pidall = $_REQUEST["bid"];
            }
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 16)->findAll("pid in ($pidall)", 'sort_id asc,id desc');
            $this->pager = spClass('goods_list')->spPager()->getPager();
        } elseif ($_REQUEST["bid"] <> "" and $_REQUEST["sid"] <> "") {
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 16)->findAll("pid like '%" . $_REQUEST["sid"] . "%'", 'sort_id asc,id desc');

            $this->pager = spClass('goods_list')->spPager()->getPager();
        } else {
            $this->goodslist = spClass('goods_list')->spPager($this->spArgs('page', 1), 16)->findAll("1=1", 'sort_id asc,id desc');

            $this->pager = spClass('goods_list')->spPager()->getPager();

        }

        $this->display("index/pr.html");
    }


    function detail()
    {


        $this->sitesystem = spClass("site_system")->find("id=1");

        $id = empty($_GET['id']) ? 33 : intval($_GET['id']);


        //$this->catlist=spClass('goods_cat')->findAll('id>0','sort_id asc,id desc');

        //$this->goodslist=spClass('goods_list')->findAll('pid='.$id,'sort_id asc,id desc');

        $this->catlist = spClass('goods_cat')->findAll('id>0', 'sort_id asc,id desc');

        $goods = spClass('goods_list')->find('id=' . $id);

        $this->xggoodslist = spClass('goods_list')->findAll('pid=' . $goods["pid"], 'sort_id asc,id desc limit 3');

        $this->cat = spClass('goods_cat')->find('id=' . $goods['pid']);

        $sql = "update my_goods set zt=zt+1 where id=$id";

        $ztrow = spClass('goods_list')->findsql($sql);

        $this->goods = $goods;


        //上一篇

        $this->previ = spClass("goods_list")->find("pid=" . $goods['pid'] . " and id >" . $id, "sort_id asc,id desc");


        //下一篇

        $this->next = spClass("goods_list")->find("pid=" . $goods['pid'] . " and id <" . $id, "sort_id asc,id desc");


        $this->display("index/product_detail.html");
    }


    function search()
    {
        $this->menuIndex = 3;

        $condition = "id>0";
        if (!empty($_REQUEST['color'])) {
            $condition .= " AND (FIND_IN_SET($_REQUEST[color],sid))";
        }


        if (!empty($_REQUEST['searkeywords'])) {
            $condition .= " AND title LIKE '%" . $_REQUEST['searkeywords'] . "%'";
        }


        $this->catlist = spClass('goods_cat')->findAll('id>0', 'sort_id asc,id desc');

        // $this->goodslist=spClass('goods_list')->findAll($condition,'sort_id asc,id desc');

        $this->goodslist = spClass('goods_list')->spPager($this->spArgs('p', 1), 120)->findAll($condition, 'sort_id asc,id desc');

        $this->pager = spClass('goods_list')->spPager()->getPager();

        $this->color = $_REQUEST['color'];

        $this->goodscolor = spClass('goods_color')->findAll('pid=1', 'sort_id asc,id desc');

        //$this->cat=spClass('goods_cat')->find('id='.$id);


        $this->display("index/pr.html");
    }


    //获取所有类及其子类
    var $allCate = array();

    function getCate($pid = 0, $level = 0)
    {
        $data = spClass('goods_cat')->findAll('pid=' . $pid, 'sort_id asc');
        if ($data) {
            foreach ($data as $v) {
                $v['level'] = $level + 1;
                $v['catename'] = str_repeat('&nbsp;', $v['level'] * 4) . $v['catename'];
                $this->allCate[] = $v;
                self::getCate($v['id'], $v['level']);
            }
        }
        return $this->allCate;
    }
}	
